package net.schmurgon.gwt.ui;

import com.google.gwt.app.place.AbstractRecordEditActivity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.RecordEditView;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RecordRequest;
import com.google.gwt.requestfactory.shared.RequestObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.valuestore.shared.SyncResult;
import com.google.gwt.valuestore.shared.Value;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.shared.Gender;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.scaffold.generated.RunnerEditView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * An activity that requests all info on an Runner, allows the user to edit
 * it, and persists the results.
 */
public class RunnerEditActivity extends AbstractRecordEditActivity<RunnerRecord> {
	private static RecordEditView<RunnerRecord> defaultView;

	private static RecordEditView<RunnerRecord> getDefaultView() {
		if (defaultView == null) {
			defaultView = new RunnerEditView();
		}
		return defaultView;
	}

	private final ApplicationRequestFactory requests;

	/**
	 * Creates an activity that uses the default singleton view instance.
	 */
	public RunnerEditActivity(RunnerRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		this(getDefaultView(), proxy, requests, placeController, creating);
	}

	/**
	 * Creates an activity that uses its own view instance.
	 */
	public RunnerEditActivity(RecordEditView<RunnerRecord> view, RunnerRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		super(view, proxy, RunnerRecord.class, creating, requests, placeController);
		this.requests = requests;
	}

    @Override
    public void start(Display display, EventBus eventBus) {
    
    getRunnerEditView().setGenderPickerValues(Arrays.asList(Gender.values()));

    
    
        super.start(display, eventBus);
    }


	@Override
	protected RecordRequest<RunnerRecord> getFindRequest(Value<Long> id) {
	    return requests.runnerRequest().findRunner(id);
	}

	protected RequestObject<Void> getPersistRequest(RunnerRecord record) {
	    return requests.runnerRequest().persist(record);
	}

    private RunnerEditView getRunnerEditView() {
        return ((RunnerEditView) getView());
    }
}
